<template>
  <el-row align="middle">
     <el-col :span="6">
         <div>
          <el-image v-if="userImageUrl" style="width:300px;height:300rpx" :src="userImageUrl" fit="fill"></el-image>
          <el-icon v-else :size="300">
           <Avatar />
          </el-icon>
         </div>
    </el-col>
    <el-col :offset="2" :span="10" style="text-align:left;">
         <div>
          <el-image v-if="imageUrl" style="width:100px;height:100px;" :src="imageUrl" fit="fill"></el-image>
          <el-icon v-else :size="100">
           <FullScreen />
          </el-icon>
        </div>
    </el-col>
    <el-upload
      ref="upload"
      class="upload-demo"
      action="https://run.mocky,io/v3/9d059bf9-4660-45f2-925d-ce80ad6c4d15"
      :limit="1"
      :on-change="handleFileChange"
      :auto-upload="false">
      <template #trigger>
        <el-button type="primary">选择照片</el-button>
      </template>
      <el-button class="ml-3" type="success" @click="submitUpload" :disabled="uploadDisable" :load="uploadLoading">
         上传至服务器
      </el-button>
    </el-upload>
  </el-row>


</template>
  
  <script lang="ts" setup>
  import { ref,onMounted } from 'vue'
  import { ElMessage } from 'element-plus'
  import { FullScreen, UploadFilled } from '@element-plus/icons-vue';
  import type { UploadInstance,UploadFile,UploadFiles } from 'element-plus';
  import {uploadUserPic,downloadUserPic} from '@/api/fileUpload'

  const upload = ref<UploadInstance>()
  const imageUrl = ref('')

  const files:any = ref()

  const uploadDisable = ref(true)
  const uploadLoading = ref(false)

  const handleFileChange = (uploadFile:UploadFile,UploadFiles:UploadFiles)=>{
    const type = ['image/png'];
    const extFileName:string = uploadFile.raw?.type as string;
    const extFileSize:Number = uploadFile.raw?.size as Number;
    console.log(uploadFile.raw)
    if(type.indexOf(extFileName)===-1){
      ElMessage({
        type:'warning',
        message:'文件类型必须是png图片文件'
      })
      upload.value!.clearFiles()
      return false;
    }else if(extFileSize >500000){
      ElMessage({
        type:'warning',
        message:'文件大小不能大于500K'
      })
      upload.value!.clearFiles()
      return false;
    }
    imageUrl.value = window.URL.createObjectURL(uploadFile.raw as Blob)
    files.value = uploadFile.raw;
    uploadDisable.value = false;
    upload.value!.clearFiles()
  }

  const submitUpload = async() => {
    console.log("fileUse",files.value)
    let formData = new FormData();
    formData.append('file',files.value)
    formData.append('useType','1')
    // console.log("formData",formData);
    uploadLoading.value = true;
    try{
      const uploadImageResult:any = await uploadUserPic(formData);
      if(uploadImageResult.success){
        imageUrl.value='';
        uploadDisable.value = true;
        ElMessage({
          type:'warning',
          message:'上传图片成功'
        })
        getUserPic()
      }else{
        ElMessage({
          type:'warning',
          message:'上传图片失败'
        })
      }
      uploadLoading.value = false;
    }catch(error){
      uploadLoading.value = false;
      console.log(error);
      ElMessage({
          type:'warning',
          message:'上传图片失败'
        })   
    }

  }

  const userImageUrl = ref()
  const getUserPic =async () => {
    try{
      const downFileResult:any = await downloadUserPic({});
      console.log("downFileResult",downFileResult);
      if(downFileResult.size>5){
        let blobFile = new Blob([downFileResult],{type:'image/png'})
        userImageUrl.value = window.URL.createObjectURL(blobFile)
        console.log(userImageUrl.value);
        
      }else{
        ElMessage({
          type:'warning',
          message:'未获得到下载文件数据'
        })
      }
    }catch(error){
      console.log(error);
      ElMessage({
          type:'warning',
          message:'下载用户图片失败'
        })
    }
  }
 onMounted(async() =>{
  getUserPic();
 })

  </script>
  
  <style scoped>
  .avatar-uploader .avatar {
    width: 178px;
    height: 178px;
    display: block;
  }
  .avatar-uploader .el-upload {
    border: 1px dashed var(--el-border-color);
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
    transition: var(--el-transition-duration-fast);
  }
  
  .avatar-uploader .el-upload:hover {
    border-color: var(--el-color-primary);
  }
  
  .el-icon.avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 178px;
    height: 178px;
    text-align: center;
  }


  </style>
  
  